package com.qdlc.p2p.biz.model;

import java.util.HashMap;
import java.util.Map;

import com.qdlc.p2p.biz.service.TokenStoreService;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.PropertiesUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.TokenStore;
import com.qdlc.p2p.dal.dto.User;

public class AppOauthTokenModel {
	
	private String userName;
	
	private String pwd;
	
	private String refreshToken;
	
	private Map<String,Object> data;
	
	public Map<String,Object> validAppLogin(){
		data = new HashMap<String,Object>();
		if (StringUtil.isBlank(getUserName())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppOauthToken01"));
			return data;
		}
		if (StringUtil.isBlank(getPwd())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppOauthToken02"));
			return data;
		}
		return null;
	}
	
	/**
	 * 检查替换授权  参数有效性
	 * 
	 * @return
	 */
	public Map<String, Object> validRefreshToken() {
		data = new HashMap<String,Object>();
		if (StringUtil.isBlank(getRefreshToken())) {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppOauthToken03"));
			return data;
		}
		return null;
	}
	
	/**
	 * 生成登录授权接口的响应信息
	 * @param user
	 * @return
	 */
	public Map<String, Object> tokenInitResponse(User user){
		data = new HashMap<String,Object>();
		long userId = user.getUserId();
		TokenStoreService tokenStoreService = (TokenStoreService) BeanUtil.getBean("tokenStoreService");
		TokenStore tokenStore = tokenStoreService.findByUserId(userId);
		if (tokenStore == null){// 不存在新增
			tokenStore = new TokenStore(user);
			tokenStore = tokenStoreService.save(tokenStore);
		}
		data.put("userId", tokenStore.getUser().getUserId());
		data.put("oauthToken", tokenStore.getOauthToken());
		data.put("refreshToken", tokenStore.getRefreshToken());
		data.put("validTime", tokenStore.getExpiresIn());
		data.put("resCode", "1");
		return data;
	}
	
	/**
	 * 生成替换授权接口的响应信息
	 * @param user
	 * @return
	 */
	public Map<String, Object> tokenRefreshResponse(TokenStore tokenStore){
		data = new HashMap<String,Object>();
		if(!StringUtil.isBlank(tokenStore)){
			TokenStoreService tokenStoreService = (TokenStoreService) BeanUtil.getBean("tokenStoreService");
			tokenStore = tokenStoreService.refresh(tokenStore);
			
			data.put("userId", tokenStore.getUser().getUserId());
			data.put("oauthToken", tokenStore.getOauthToken());
			data.put("refreshToken", tokenStore.getRefreshToken());
			data.put("validTime", tokenStore.getExpiresIn());
			data.put("resCode", "1");
		} else {
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppOauthToken04"));
		}
		return data;
	}
	
	/**
	 * 
	 * @return
	 */
	public Map<String,Object> validUser(User user){
		data = new HashMap<String,Object>();
		if(StringUtil.isBlank(user)){
			data.put("resCode", "0");
			data.put("resMsg", PropertiesUtil.getValue("AppOauthToken05"));
			return data;
		}
		return null;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getRefreshToken() {
		return refreshToken;
	}

	public void setRefreshToken(String refreshToken) {
		this.refreshToken = refreshToken;
	}
}
